package com.sitia.system.service.impl;

import com.sitia.common.CustomException;
import com.sitia.common.utils.DateUtils;
import com.sitia.common.utils.SecurityUtils;
import com.sitia.common.utils.poi.ExcelUtil;
import com.sitia.common.utils.uuid.IdUtils;
import com.sitia.system.domain.MxCity;
import com.sitia.system.domain.MxProvince;
import com.sitia.system.mapper.MxCityMapper;
import com.sitia.system.mapper.MxProvinceMapper;
import com.sitia.system.service.IMxCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 城市Service业务层处理
 * 
 * @author sitia
 * @date 2023-07-16
 */
@Service
public class MxCityServiceImpl implements IMxCityService 
{
    @Autowired
    private MxCityMapper mxCityMapper;
    @Autowired
    private MxProvinceMapper provinceMapper;

    /**
     * 查询城市
     * 
     * @param id 城市主键
     * @return 城市
     */
    @Override
    public MxCity selectMxCityById(String id)
    {
        return mxCityMapper.selectMxCityById(id);
    }

    /**
     * 查询城市列表
     * 
     * @param mxCity 城市
     * @return 城市
     */
    @Override
    public List<MxCity> selectMxCityList(MxCity mxCity)
    {
        return mxCityMapper.selectMxCityList(mxCity);
    }

    /**
     * 新增城市
     * 
     * @param mxCity 城市
     * @return 结果
     */
    @Override
    public int insertMxCity(MxCity mxCity)
    {
        mxCity.setCreateTime(DateUtils.getNowDate());
        return mxCityMapper.insertMxCity(mxCity);
    }

    /**
     * 修改城市
     * 
     * @param mxCity 城市
     * @return 结果
     */
    @Override
    public int updateMxCity(MxCity mxCity)
    {
        return mxCityMapper.updateMxCity(mxCity);
    }

    /**
     * 批量删除城市
     * 
     * @param ids 需要删除的城市主键
     * @return 结果
     */
    @Override
    public int deleteMxCityByIds(String[] ids)
    {
        return mxCityMapper.deleteMxCityByIds(ids);
    }

    /**
     * 删除城市信息
     * 
     * @param id 城市主键
     * @return 结果
     */
    @Override
    public int deleteMxCityById(String id)
    {
        return mxCityMapper.deleteMxCityById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importExcelData(MultipartFile file) {
        if (!(file.getOriginalFilename().endsWith(".xlsx")||file.getOriginalFilename().endsWith(".xls"))) {
            throw new CustomException("请上传.xlsx或.xls结尾的excel文件");
        }

        ExcelUtil<MxCity> util = new ExcelUtil<MxCity>(MxCity.class);
        List<MxCity> list = null;
        try {
             list = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("导入失败");
        }
        if (list==null||CollectionUtils.isEmpty(list)) {
            throw new CustomException("没有导入数据");
        }
        Long provId = null;
        Long userId = SecurityUtils.getUserId();
        Date now = DateUtils.getNowDate();
        for (MxCity city : list) {
            if (city.getCity().trim().equals("全部")) {
                //插入省份
                MxProvince mxProvince = new MxProvince();
                mxProvince.setCode(city.getCityCode()).setProvince(city.getProvinceName()).setCreateBy(userId.toString());
                mxProvince.setCreateTime(now);
                int i = provinceMapper.insertMxProvince(mxProvince);
                provId = mxProvince.getId();
            }
            //添加城市
            city.setId(IdUtils.simpleUUID()).setProvinceId(provId).setCreateTime(now);
            city.setCreateBy(userId.toString());
            mxCityMapper.insertMxCity(city);
        }
        return "导入成功";
    }
}
